package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserSubscribe;

import java.sql.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserSubscribeDaoImpl extends TempDataObjectDao<UserSubscribe> {

    public Map<Integer, UserSubscribe> getUserSubscribeMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_subscribe where userId=?";
        Map<Integer, UserSubscribe> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserSubscribe data = parse(rs);
                dataMap.put(data.getMallId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(UserSubscribe data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_subscribe(userId,mallId,orderNo,type,isFree,expiresTime,modifyTime)" +
                " values (?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getMallId());
            call.setString(i++, data.getOrderNo());
            call.setInt(i++, data.getType());
            call.setBoolean(i++, data.isFree());
            call.setLong(i++, data.getExpiresTime());
            call.setTimestamp(i++, new Timestamp(data.getModifyTime().getTime()));
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserSubscribe data) {
        PreparedStatement call = null;
        String sql = "update t_u_subscribe set orderNo=?,type=?,isFree=?,expiresTime=?,modifyTime=? " +
                " where userId=? and mallId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, data.getOrderNo());
            call.setInt(i++, data.getType());
            call.setBoolean(i++, data.isFree());
            call.setLong(i++, data.getExpiresTime());
            call.setTimestamp(i++, new Timestamp(data.getModifyTime().getTime()));
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getMallId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserSubscribe parseData(ResultSet rs) throws SQLException {
        UserSubscribe data = new UserSubscribe();
        data.setUserId(rs.getLong("userId"));
        data.setMallId(rs.getInt("mallId"));
        data.setOrderNo(rs.getString("orderNo"));
        data.setType(rs.getInt("type"));
        data.setFree(rs.getBoolean("isFree"));
        data.setExpiresTime(rs.getLong("expiresTime"));
        data.setModifyTime(new Date(rs.getTimestamp("modifyTime").getTime()));
        return data;
    }
}
